#include "SoundManager.h"


SoundManager::SoundManager()
{
}


SoundManager::~SoundManager()
{
	clear();
}

bool SoundManager::loadSound(const string &name){
	SoundBuffer *current = new SoundBuffer;

	bool success = current->loadFromFile(soundsPath+name);

	if(!success){
		delete current;
		return false;
	}

	// If a sound with the same name already exists
	// we delete it and replace it.
	if(m_sounds.find(name) != m_sounds.end()){
		delete m_sounds[name];
	}

	m_sounds[name] = current;

	return true;
}


SoundBuffer* SoundManager::getSoundBuffer(const string &name){
	return m_sounds[name];
}

void SoundManager::playSound(const string &name){
	for(int i = 0; i < MAX_SOUNDS; i++){
		if(m_soundVector[i].getStatus()==Sound::Stopped){
			m_soundVector[i].setBuffer(*getSoundBuffer(name));
			m_soundVector[i].play();
			break;
		}
	}
}


void SoundManager::clear(){
	map<string, SoundBuffer*>::iterator it;
	for(it = m_sounds.begin(); it!=m_sounds.end(); ++it){
		delete it->second;
	}

	m_sounds.clear();
}